package com.kika.obwiki.config;

import com.kika.obwiki.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {

  @Autowired
  private LoginInterceptor loginInterceptor;

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(loginInterceptor)
        .addPathPatterns("/**")
        .excludePathPatterns(
            "/test/**",
            "/redis/**",
            "/user/login",
            "/category/all",
            "/ebook/list",
            "/doc/all/**",
            "/doc/vote/**",
            "/content/findContent/**",
            "/ebookSnapshot/**",
            "/websocket/**");
  }

  /**
   * 静态资源映射，支持 /upload/** 和 /image/**
   * /upload/** -> D:/upload/
   * /image/** -> D:/upload/（兼容历史数据）
   * 这样前端无论是 /upload/xxx.jpg 还是 /image/xxx.jpg 都能访问到
   */
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/upload/**")
        .addResourceLocations("file:D:/upload/");
    registry.addResourceHandler("/image/**")
        .addResourceLocations("file:D:/upload/");
  }
}